package com.dr.service.impl;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.dr.mapper.UserMapper;
import com.dr.pojo.User;
import com.dr.service.UserService;

@Service
@Transactional
public class UserServiceImpl implements UserService {
	@Autowired
	private UserMapper mapper;

	@Override
	public List<User> findUserList() {
		return mapper.findUserList();
	}

	@Override
	public User findUserById(Integer id) {
		return mapper.findUserById(id);
	}

	@Override
	public List<User> findUserByName(String name) {
		return mapper.findUserByName(name);
	}
	
	@Override
	public int insertUser(User user) {
		Integer enable = user.getEnable();
		enable = (enable == null)?0:enable;
		user.setEnable(enable);
		return mapper.insertUser(user);
	}

	@Override
	public int insertUser(User user, HttpSession session) {
		//处理Enable
		Integer enable = user.getEnable();
		enable = (enable == null)?0:enable;
		user.setEnable(enable);
		
		//处理CreateUserId，从Session中取得当前登录的用户，作为CreateUser
		User loginUser = (User)session.getAttribute("loginUser");
		user.setCreateUserId(loginUser.getId().toString());
		return mapper.insertUser(user);
	}

	@Override
	public int deleteUser(User user) {
		return mapper.deleteUser(user);
	}
	
	@Override
	public int deleteUser(User user, HttpSession session) {
		//处理UpdateUserId，从Session中取得当前登录的用户，作为CreateUser
		User loginUser = (User)session.getAttribute("loginUser");
		user.setUpdateUserId(loginUser.getId().toString());
		return mapper.deleteUser(user);
	}
	
	@Override
	public int updateUser(User user) {
		Integer enable = user.getEnable();
		enable = (enable == null)?0:enable;
		user.setEnable(enable);
		return mapper.updateUser(user);
	}

	@Override
	public int updateUser(User user, HttpSession session) {
		//处理Enable
		Integer enable = user.getEnable();
		enable = (enable == null)?0:enable;
		user.setEnable(enable);
		
		//处理UpdateUserId，从Session中取得当前登录的用户，作为CreateUser
		User loginUser = (User)session.getAttribute("loginUser");
		user.setUpdateUserId(loginUser.getId().toString());
		return mapper.updateUser(user);
	}

	@Override
	public User findUserByAccount(String account) {
		return mapper.findUserByAccount(account);
	}
	
}